import Mock from 'mockjs'
const Random = Mock.Random;
import router from '../../router';

// var goods = []
// for(var i = 0 ; i < 3; i ++){

// }
var bannerKind = []

for (var i = 0; i < 12; i++) {
  bannerKind.push({
    id: Random.guid(),
    name: Random.name(),
    cname: Random.cname(),
    monay: Random.integer(100, 999),
    stock: Random.integer(3, 12), //库存
    img: Random.image("275x383", Random.color()),
    shuliang: 0,
    checked: true
    // total:0
  })
}
// state相当于data 存放共享的数据的
const state = {
  // count: 1,
  goodsList: bannerKind,
  myid: ""
}
//此处操作(修改)state属性的 写方法
const mutations = {
  myID(state, id) {
    state.myid = id
  },
  getPulsGoods(state, id) {
    var objPuls = state.goodsList.find(item => item.id === id)
    if (objPuls) {
      if(objPuls.stock>0){
        // objPuls.count++
        // objPuls.stock--
      }
    }
  },
  getMinusGoods(state, id) {
    var objPuls = state.goodsList.find(item => item.id === id)
    if (objPuls) {
      if (objPuls.shuliang <objPuls.stock) {
        // objPuls.count--
        // objPuls.stock++
      }

    }
  },
  // modStock(state, id) {
  //   var objStock = state.goodsList.find(item => item.id === id)
  //   if (objStock) {

  //     objStock.stock--
  //   }
  // }
}
//此处相当于计算属性,
const getters = {
  //商品列表点击传进来的数据,在传给商品详情
  myList(state) {
    var myItem = state.goodsList.filter(item => item.id === state.myid)
    return myItem
  },
}
//用于异步操作,可以调用mutations的方法,从而修改state
const actions = {

}
export default {
  //namespaced 应为都是全局的,为了防止命名冲突
  namespaced: true,
  state,
  getters,
  mutations,
  actions,

}